package client;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import chat.ChatCommunication;
//import javax.swing.*;
import com.*;
import chat.ChatProperties;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Timer;
import javax.swing.JFrame;
 

/**
 *
 * @author michaellangmayr
 */
public class UDPChatClient {
    
    private SocketConfig sc = null;
    private SequData sequData = new SequData(0, 0);
    private int msgId = 0;
    private ChatClientView view = null;
    private String nickName = null;
    private DatagramSocket socket = null;

    public static void main(String[] args){
        
        if(args.length > 1){
            String hostport = args[0];
            String[] hostportArr = hostport.split(":");
            String hostname = hostportArr[0];
            int port = Integer.parseInt(hostportArr[1]);

            UDPChatClient cl = new UDPChatClient();

            ChatProperties cp = new ChatProperties();
            try{
                cp.read();
            } catch(Exception e)
            {
                System.err.println("Couldn't read props " + e);
                System.exit(1);
            }
            int myPort = Integer.parseInt(args[1]);
            InetSocketAddress server = new InetSocketAddress(hostname, port);
            cl.sc = new SocketConfig(server, myPort, cp.getLoss()); // Achtung: loss ist hard coded
            cl.start(cl.sc, cp);

        }
        else{
            System.err.println("Usage: Client servername:serverport myport");
        }
        
    }

    public void start(SocketConfig sc, ChatProperties cp){
        ICommunication udp = null;
        if(sc != null) {
            try{
                udp = new ChatCommunication(sc, this);
            } catch(java.net.SocketException e) {
                System.err.println("Couldn't create ChatCommunication " + e);
            }
        }
        try {
            socket = new DatagramSocket(sc.getMyPort());
            getSocket().setSoTimeout(0);
        } catch (SocketException ex) {
            System.err.println("Couldn't open socket " + ex);
        }
        // Start Interface
        view = new ChatClientView(udp, this);
        getView().setVisible(true);
        
        JFrame frame = new JFrame("Chat Client");
        frame.add(getView());
        frame.pack();
        frame.setVisible(true);

        // start polling in a new Thread
        Timer timer = new Timer();
        PollingTask poll = new PollingTask(udp);
        timer.scheduleAtFixedRate(poll, 0, cp.getPolling());

        ClientListener clientListener = null;
        try {
            clientListener = new ClientListener(this);
        } catch (SocketException ex) {
            System.err.println("Couldn't create ClientListener " + ex);
        }
        clientListener.start();
    }

    /**
     * @return the sequData
     */
    public SequData getSequData() {
        return sequData;
    }

    /**
     * @return the msgId
     */
    public int getMsgId() {
        return msgId;
    }

    /**
     * @param msgId the msgId to set
     */
    public void setMsgId(int msgId) {
        this.msgId = msgId;
    }

    /**
     * @return the view
     */
    public ChatClientView getView() {
        return view;
    }

    /**
     * @return the nickName
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * @param nickName the nickName to set
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    /**
     * @return the socket
     */
    public DatagramSocket getSocket() {
        return socket;
    }

}
